package com.totoro.mysql;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;

public class OneInsertTest {
    private static String createSql(int num){
        StringBuffer sql = new StringBuffer("INSERT INTO test_table (name) values ") ;
        for(int i=0; i<num; i++){
            String value = "(".concat(addSemicolon("name"+i)).concat("),") ;
            sql.append(value) ;
        }
        String sqlStr = sql.toString() ;
        if(sqlStr.endsWith(","))
            sqlStr = sqlStr.substring(0, sqlStr.length()-1) ;
        return sqlStr ;
    }

    private static String addSemicolon(String value){
        return "'".concat(value).concat("'") ;
    }

    public static void test() throws SQLException {
        long begin = System.currentTimeMillis() ;
        String sql = createSql(10000) ;
        Connection connection = ConnectionUtil.getConnection() ;
        Statement statement = connection.createStatement() ;
        statement.execute(sql) ;
        //PreparedStatement preparedStatement = connection.prepareStatement(sql) ;
        long end = System.currentTimeMillis() ;
        System.out.println("方法共用时:"+(end-begin)+"毫秒！");
        connection.commit();
        connection.close();
    }

    public static void main(String[] args) {
        try {
            test() ;
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}
